package com.easyUploader.controller;

import com.common.validator.CustomNotEmpty;
import com.easyUploader.entity.dto.UserDto;
import com.easyUploader.entity.vo.UserUpdateVo;
import com.easyUploader.entity.vo.UserVo;
import com.easyUploader.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import com.common.result.ResponseResultAdvice;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import static com.common.util.PatternUtil.EMAIL_PATTERN;
import static com.security.filter.AbstractTokenAuthenticationFilter.NO_TOKEN_AUTH_REQUEST_PREFIX;

/**
 * @author 大忽悠
 * @create 2022/9/15 11:09
 */
@Validated
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@ResponseResultAdvice
@Api(tags = "用户请求")
public class UserController {
    private final IUserService iUserService;

    @ApiOperation("发送验证码")
    @PostMapping(NO_TOKEN_AUTH_REQUEST_PREFIX+"sendCode")
    public void sendCode(@Pattern(regexp = EMAIL_PATTERN, message = "邮箱格式错误")@RequestParam("eamil") String eamil) {
        iUserService.sendCode(eamil);
    }

    @ApiOperation("用户注册")
    @PostMapping(NO_TOKEN_AUTH_REQUEST_PREFIX+"register")
    public String userRegister(@Valid UserVo userVo) throws IOException {
        return iUserService.register(userVo);
    }

    @ApiOperation("用户登录")
    @PostMapping(NO_TOKEN_AUTH_REQUEST_PREFIX+"login")
    public String userLogin(@RequestParam("username")@CustomNotEmpty(message = "用户名不能为空") String username,@RequestParam("password")@CustomNotEmpty(message = "用户名不能为空") String password)  {
        return iUserService.login(username,password);
    }

    @ApiOperation("用户信息修改")
    @PutMapping("/update")
    public void userInfoUpdate(@Valid UserUpdateVo userUpdateVo) throws IOException {
        iUserService.updateUser(userUpdateVo);
    }

    @ApiOperation("注销用户")
    @DeleteMapping("/cancel")
    public void userCancel(){
        iUserService.userCancel();
    }

    @ApiOperation("查询个人用户信息")
    @GetMapping
    public UserDto userInfo() {
        return iUserService.userInfo();
    }

    @ApiOperation("查询个人头像数据")
    @GetMapping(value = "/headImgData")
    public void userHeadImgData(HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest) throws UnsupportedEncodingException {
        iUserService.getHeadImgData(httpServletResponse,httpServletRequest);
    }
}
